//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// (C) Copyright 2006 Marvell International Ltd.
// All Rights Reserved
//
/******************************************************************************
**
** INTEL CONFIDENTIAL
** Copyright 2003-2005 Intel Corporation All Rights Reserved.
**
** The source code contained or described herein and all documents
** related to the source code (Material) are owned by Intel Corporation
** or its suppliers or licensors.  Title to the Material remains with
** Intel Corporation or its suppliers and licensors. The Material contains
** trade secrets and proprietary and confidential information of Intel
** or its suppliers and licensors. The Material is protected by worldwide
** copyright and trade secret laws and treaty provisions. No part of the
** Material may be used, copied, reproduced, modified, published, uploaded,
** posted, transmitted, distributed, or disclosed in any way without Intel's
** prior express written permission.
**
** No license under any patent, copyright, trade secret or other intellectual
** property right is granted to or conferred upon you by disclosure or
** delivery of the Materials, either expressly, by implication, inducement,
** estoppel or otherwise. Any license under such intellectual property rights
** must be express and approved by Intel in writing.
**
**
******************************************************************************/

/****************************************************************
*    FAT.h
*       This is the header file for the FAT fs reader that is 
*       used with eboot to read and copy images off of SD/MMC 
*       cards.
*
*    Revision History:
*       Jason White - 1/18/06 - initial draft
****************************************************************/

#include "xllp_mmc.h"

#define FAT12 0
#define FAT16 1
#define FAT32 2

#define ACTIVE_PARTITION 0x80
#define END_OF_FILE 0xFFF8
#define ROOT_SIZE 32
#define MBR 0

#define PARTITION_1 1
#define PARTITION_2 2
#define PARTITION_3 3
#define PARTITION_4 4
#define NO_PARTITION 0

typedef struct
{
    BYTE BS_jmpBoot[3];
    BYTE BS_OEMName[8];
    BYTE BPB_BytsPerSec[2];
    BYTE BPB_SecPerClus;
    BYTE BPB_RsvdSecCnt[2];
    BYTE BPB_NumFATs;
    BYTE BPB_RootEntCnt[2];
    BYTE BPB_TotSec16[2];
    BYTE BPB_Media;
    BYTE BPB_FATSz16[2];
    BYTE BPB_SecPerTrk[2];
    BYTE BPB_NumHeads[2];
    BYTE BPB_HiddSec[4];
    BYTE BPB_TotSec32[4];
    BYTE BPB_DrvNum;
    BYTE BPB_Reserved1;
    BYTE BPB_BootSig;
    BYTE BPB_VolID[4];
    BYTE BPB_VolLab[11];
    BYTE BPB_FilSysType[8];
} BPB_STRUCTURE, *P_BPB_STRUCTURE;

typedef struct
{
    XLLP_UINT32_T RootDirsSectors;
    XLLP_UINT32_T FirstDataSector;
    XLLP_UINT32_T FirstSectorofCluster;
    XLLP_UINT32_T FirstRootDirSecNum;
    XLLP_UINT32_T FATSz;
    XLLP_UINT32_T TotSec;
    XLLP_UINT32_T DataSec;
    XLLP_UINT32_T CountOfClusters;
    XLLP_UINT32_T FATType;
} FAT_STRUCTURE, *P_FAT_STRUCTURE;

typedef struct
{
    BYTE active;
    BYTE startHead;
    BYTE startSec[2];
    BYTE type;
    BYTE endHead;
    BYTE endSec[2];
    BYTE secBeforePart[4];
    BYTE numSec[4];
} PARTITION, *P_PARTITION;

typedef struct
{
    PARTITION partition1;
    PARTITION partition2;
    PARTITION partition3;
    PARTITION partition4;
} MBR_STRUCTURE, *P_MBR_STRUCTURE;

typedef struct
{
    BYTE DIR_Name[11];
    BYTE DIR_Attr;
    BYTE DIR_NTRes;
    BYTE DIR_CrtTimeTenth;
    BYTE DIR_CrtTime[2];
    BYTE DIR_CrtDate[2];
    BYTE DIR_LstAccDate[2];
    BYTE DIR_FstClusHI[2];
    BYTE DIR_WrtTime[2];
    BYTE DIR_WrtDate[2];
    BYTE DIR_FstClusLO[2];
    BYTE DIR_FileSize[4];
} DIR_STRUCTURE, *P_DIR_STRUCTURE;

typedef struct
{
    XLLP_UINT32_T ThisFATSecNum;
    XLLP_UINT32_T ThisFATEntOffset;
} READ_FAT, *P_READ_FAT;

BOOL fillBootBlockStructure(P_BPB_STRUCTURE pBPB, BYTE *buffer);
void getFATDataStructure(P_BPB_STRUCTURE pBPB, P_FAT_STRUCTURE pFAT);
void DumpFATStructure(P_FAT_STRUCTURE pFAT);
void getSectorOffset(P_BPB_STRUCTURE pBPB, P_FAT_STRUCTURE pFAT,
                     P_READ_FAT pReadFAT, XLLP_UINT32_T cluster);
BOOL isEOF(XLLP_UINT32_T FATType, XLLP_UINT32_T sector);
void getShortDirStructure(P_DIR_STRUCTURE pDIR, BYTE *buffer);
void getMBR(P_MBR_STRUCTURE pMBR, BYTE *buffer);
void getPartition(P_PARTITION pPART, BYTE *buffer);
int findActivePartition(P_MBR_STRUCTURE pMBR);
XLLP_UINT32_T computeSectorsBeforePartition(P_PARTITION pPART);
